function [raw,normali, xaxis,dirsave]=findRobustWeightsdraw(TF,w,h,numparams,corrs)


numpoints=100;
minscale=1;
maxscale=30;
xvectors=minscale:((maxscale-minscale)/(numpoints-1)):maxscale;


[~,numFs]=size(TF);

numTries=1000;

raw=repmat({zeros(numTries,numFs)}, numpoints, 1);
normali=repmat({zeros(numTries,numFs)}, numpoints, 1);


xstart=w/2;
ystart=h/2;
fstart=w;

tick=1;
for k=1:1:numpoints
    xval=xvectors(k);
    
    fstd=1;
    xstd=xstart/200;
    ystd=ystart/200;
    skewstd=0.01;
    arstd=0.01;
    
    
    fstd=fstd* xval;
    xstd=xstd* xval;
    ystd=ystd* xval;
    skewstd=skewstd* xval;
    arstd=arstd* xval;
    
    for i=1:numTries
        focal=fstart+(randn()*fstd);
        ar=(1+rand()*arstd);
        skew=skewstd*randn();
        xguess=xstart+(randn()*xstd);
        yguess=ystart+(randn()*ystd);
        Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];
        Kng=convertXTOKselfK(convertKTOXselfK(Kguess,1),w,h);
                Kng2=convertXTOKselfK(convertKTOXselfK(Kguess,5),w,h);

        for j=1:numFs
            
            [FF,s1,s2] = computerEssentialErrorSVDparts(TF{1,j},Kng,Kng');
            [FFz,s1,s2] = computerEssentialErrorSVDparts(TF{1,j},Kng2,Kng2');
            raw{tick,1}(i,j)=FF;
            normali{tick,1}(i,j)=FFz;
            
        end
        
    end
    
    xaxis(tick)= xval;
    tick=tick+1;
end

dirsave=drawquantities(raw,normali, xaxis);


end
function [dirsave]=drawquantities(raw,normalized,xaxis)
styles={'-.or' ,'-.xg', '-.+b', '-.*y', '-.vr' ,'-..c'};

drawdirname='draw3/';
if(exist(drawdirname,'dir')==0)
    mkdir(drawdirname);
end

flagexist=1;
count=1;
newmydirname=[];
while(flagexist==1)
    if(exist([drawdirname 'dir_' num2str(count)],'dir')~=0)
        count= count+1;
        flagexist=1;
    else
        newmydirname=[drawdirname 'dir_' num2str(count) '/'];
        flagexist=0;
    end
end
mkdir([newmydirname 'png']);
mkdir([newmydirname 'jpg']);
mkdir([newmydirname 'eps']);
mkdir([newmydirname 'fig']);
mkdir([newmydirname 'matlabf']);
dirsave=[newmydirname 'matlabf/'];
% mymeasure.errortype={'norm','raw'};
% mymeasure.operateson={'cov','ptest','cor','T'};
% mymeasure.preprocess={'abs','none'};
% mymeasure.measure={'mean','median','std'};
% mymeasure.show={'mean','median','max','min'};

mymeasure.errortype={'norm','raw'};
mymeasure.operateson={'cov','T'};
mymeasure.preprocess={'none','abs'};
mymeasure.measure={'mean','std'};
mymeasure.show={'mean','median','max','min'};

lowpercentage=0.1;

[numPoints,n]=size(raw);



[~,numf]=size(normalized{1});
badf=zeros(1,numf);badf(1,1)=1; % i decided the first F is the bad one
operations=zeros(size(mymeasure.errortype,2),numPoints,size(mymeasure.operateson,2),size(mymeasure.preprocess,2),size(mymeasure.measure,2));
operationsothers=zeros(size(mymeasure.errortype,2),numPoints,size(mymeasure.operateson,2),size(mymeasure.preprocess,2),size(mymeasure.measure,2),size(mymeasure.show,2));



for k=1:size(mymeasure.errortype,2)
    
    if(strcmpi(mymeasure.errortype{k},'norm')==1)
        majorMat=normalized;
    elseif(strcmpi(mymeasure.errortype{k},'raw')==1)
        majorMat=raw;
    end
    
    for tt=1:numPoints
        mat=majorMat{tt,1};
        
        for i=1:size(mymeasure.operateson,2)
            if(strcmpi(mymeasure.operateson{i},'cov')==1)
                TM=cov(mat);
            elseif(strcmpi(mymeasure.operateson{i},'ptest')==1)
                [~,TM] = corrcoef(mat);
            elseif(strcmpi(mymeasure.operateson{i},'cor')==1)
                [TM,~] = corrcoef(mat);
            elseif(strcmpi(mymeasure.operateson{i},'T')==1)
                TM=mat;
            end
            for j=1:size(mymeasure.preprocess,2)
                if(strcmpi(mymeasure.preprocess{j},'abs')==1)
                    TM=abs(TM);
                elseif(strcmpi(mymeasure.preprocess{j},'none')==1)
                    TM=(TM);
                end
                for q=1:size(mymeasure.measure,2)
                    if(strcmpi(mymeasure.measure{q},'mean')==1)
                        nn=mean(TM);
                    elseif(strcmpi(mymeasure.measure{q},'median')==1)
                        nn=median(TM);
                    elseif(strcmpi(mymeasure.measure{q},'std')==1)
                        nn=std(TM);
                    end
                    
                    baddats=nn(1,badf==1);
                    gooddats=nn(1,(~badf)==1);
                    
                    operations(k,tt,i,j,q)=mean(baddats);
                    
                    for l=1:size(mymeasure.show,2)
                        if(strcmpi(mymeasure.show{l},'mean')==1)
                            pp=mean(gooddats);
                        elseif(strcmpi(mymeasure.show{l},'median')==1)
                            pp=median(gooddats);
                        elseif(strcmpi(mymeasure.show{l},'max')==1)
                            pp=max(gooddats);
                        elseif(strcmpi(mymeasure.show{l},'min')==1)
                            pp=min(gooddats);
                            
                        end
                        
                        operationsothers(k,tt,i,j,q,l)=pp;
                    end
                    
                    
                    
                end
                
            end
        end
    end
    
end

%%%%%%%%%%%%%%%%%%%%%%%%
plotnum=1;
numplotz=1;
for k=1:size(mymeasure.errortype,2)
    for i=1:size(mymeasure.operateson,2)
        for j=1:size(mymeasure.preprocess,2)
            
            for q=1:size(mymeasure.measure,2)
                numplotz=numplotz+1;
            end
        end
    end
    
end




for k=1:size(mymeasure.errortype,2)
    for i=1:size(mymeasure.operateson,2)
        for j=1:size(mymeasure.preprocess,2)
            
            for q=1:size(mymeasure.measure,2)
                
                gcf = figure('Visible','off');
                hold;
                plot(xaxis,operations(k,:,i,j,q),styles{1});
                legstring=cell(1,size(mymeasure.show,2)+1);
                legstring{1,1}='my graph';
                appendtext='';
                for l=1:size(mymeasure.show,2)
                    if(strcmpi(mymeasure.show{l},'mean')==1)
                        legstring{l+1}='mean';
                    elseif(strcmpi(mymeasure.show{l},'median')==1)
                        legstring{l+1}='median';
                    elseif(strcmpi(mymeasure.show{l},'max')==1)
                        legstring{l+1}='max';
                        prcntMax=sum(operations(k,:,i,j,q)<operationsothers(k,:,i,j,q,l))/numPoints;
                        if(prcntMax<lowpercentage)
                            display('bigger than all the max');
                            appendtext='WINNINGMAX_';
                        end
                    elseif(strcmpi(mymeasure.show{l},'min')==1)
                        legstring{l+1}='min';
                        prcntMin=sum(operations(k,:,i,j,q)>operationsothers(k,:,i,j,q,l))/numPoints;
                        if(prcntMin<lowpercentage)
                            display('smaller than all the min');
                            appendtext='WINNINGMIN_';
                        end
                    end
                    plot(xaxis,operationsothers(k,:,i,j,q,l),styles{l+1});
                end

                xlabel(['the multiple of variance']);       %  add axis labels and plot title
                ylabel([mymeasure.measure{q} ' of ' mymeasure.operateson{i } ' calculated from ' mymeasure.errortype{k} ' errors preprocessed with ' mymeasure.preprocess{j} ] );
                title({[' ERROR: '  mymeasure.errortype{k} ' -- OPERATION: ' mymeasure.operateson{i } ' -- PREPROCESS: ' mymeasure.preprocess{j} ' -- MEASURE: ' mymeasure.measure{q} ],['min power=' num2str(prcntMin) ' and max power = ' num2str(prcntMax)]});
                legend(legstring);
                
                %  saveas(gcf,['param' paramcheck '_' dataNames{i} nowtime '.eps']);
                saveas(gcf,[ newmydirname 'png/' appendtext 'draw_'  mymeasure.errortype{k} '_op_' mymeasure.operateson{i } '_pre_' mymeasure.preprocess{j} '_measure_' mymeasure.measure{q} '.png']);
             %   saveas(gcf,[ newmydirname 'jpg/' appendtext 'draw_'  mymeasure.errortype{k} '_op_' mymeasure.operateson{i } '_pre_' mymeasure.preprocess{j} '_measure_' mymeasure.measure{q} '.jpg']);
             %   saveas(gcf,[ newmydirname 'fig/' appendtext 'draw_'  mymeasure.errortype{k} '_op_' mymeasure.operateson{i } '_pre_' mymeasure.preprocess{j} '_measure_' mymeasure.measure{q} '.fig']);
             %   saveas(gcf,[ newmydirname 'eps/' appendtext 'draw_'  mymeasure.errortype{k} '_op_' mymeasure.operateson{i } '_pre_' mymeasure.preprocess{j} '_measure_' mymeasure.measure{q} '.eps'],'epsc');
                hold
                plotnum=plotnum+1;
                
            end
        end
    end
    
end

%save([newmydirname 'files.mat'], 'raw', 'normalized','xaxis', 'mymeasure');
%copyfile('*.m',[newmydirname 'matlabf']);

% saveas(gcf,['plotsdraw.jpg']);
% saveas(gcf,['plotsdraw.fig']);
% saveas(gcf,['plotsdraw.eps'],'epsc');

end

function [F,s1,s2] = computerEssentialErrorSVDparts(MYF,K1,K1T)

if(isempty(MYF)==1)
    F=0;
    return;
end

G=(K1T)*MYF*K1;

try
    S = svd(G);
catch
    S= [ 100 ;  10 ;  5];
end

if( S(2,1)>eps)
    F=(((S(1,1)-S(2,1))/S(2,1))); % should not be squared but all my thresholds are based on this being squared
else
    
    F=100;
end

s1=S(1,1);
s2=S(2,1);

end
